package org.lkp.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.lkp.reggie.mapper.CategoryMapper;
import org.lkp.reggie.pojo.entity.Category;
import org.lkp.reggie.pojo.entity.Dish;
import org.lkp.reggie.pojo.entity.Setmeal;
import org.lkp.reggie.response.CustomException;
import org.lkp.reggie.service.CategoryService;
import org.lkp.reggie.service.DishService;
import org.lkp.reggie.service.SetmealService;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    private final DishService dishService;
    private final SetmealService setmealservice;
    //删除分类,删除之前需要判断是否关联了菜品，删除之前需要判断是否关联了套餐
    @Override
    public void remove(Long id) {
        //判断是否关联了菜品
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId,id);
        long count1 = dishService.count(dishLambdaQueryWrapper);
        if(count1>0){
            throw new CustomException("当前分类下关联了菜品，不能删除");
        }
        //判断是否关联了套餐
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId,id);
        long count2 = setmealservice.count(setmealLambdaQueryWrapper);
        if (count2>0){
            throw new CustomException("当前分类下关联了套餐，不能删除");
        }
        removeById(id);
    }
}
